package cn.elwy.rcp.ui.widget;

import org.eclipse.jface.action.ContributionItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class ToolTextItem extends ContributionItem {

  private ToolItem toolitem;
  private Text text;
  private CLabel cLabel;
  private String label;

  public ToolTextItem(ToolBar parent, int style) {
    toolitem = new ToolItem(parent, SWT.SEPARATOR);
    Control control = createControl(parent, style);
    toolitem.setControl(control);
    setWidth(200);
  }

  public ToolTextItem(ToolBar parent, int style, String label) {
    this.label = label;
    toolitem = new ToolItem(parent, SWT.SEPARATOR);
    Control control = createControl(parent, style);
    toolitem.setControl(control);
    setWidth(200);
  }

  /**
   * @wbp.parser.entryPoint
   */
  protected Control createControl(Composite parent, int style) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new FormLayout());

    cLabel = new CLabel(composite, SWT.NONE);
    FormData fd_cLabel = new FormData();
    fd_cLabel.top = new FormAttachment(0);
    fd_cLabel.left = new FormAttachment(0);
    if (label == null) {
      fd_cLabel.right = new FormAttachment(0);
    } else {
      cLabel.setText(label);
    }
    fd_cLabel.bottom = new FormAttachment(100);
    cLabel.setLayoutData(fd_cLabel);

    text = new Text(composite, SWT.BORDER & style);
    FormData fd_text = new FormData();
    fd_text.left = new FormAttachment(cLabel, 2);
    fd_text.top = new FormAttachment(50, -12);
    fd_text.right = new FormAttachment(100, -1);
    fd_text.bottom = new FormAttachment(50, 12);
    text.setLayoutData(fd_text);
    return composite;
  }

  public void fill(ToolBar parent, int index) {
  }

  public Control getControl() {
    return text;
  }

  public String getLabel() {
    return cLabel.getText();
  }

  public String getText() {
    return text.getText();
  }

  public void setText(String text) {
    this.text.setText(text);
  }

  public void setWidth(int width) {
    toolitem.setWidth(width);
  }
}